<?php


function unsCalendar_addClassroom($name, $capacity, $buildingID, $videoprojector) {

    global $babDB, $babBody;

    if (null != $name && null != $capacity && null != $buildingID && null != $videoprojector) {

        if (unsCalendar_getRessourceCalendarIDForName($name) == 0) {

            $availability_lock = '1';

            $babDB->db_query("
		insert into " . BAB_CAL_RESOURCES_TBL . " 
			(name, description, id_dgowner, idsa, availability_lock) 
		VALUES 
			(
				'" . $babDB->db_escape_string($name) . "', 
				' ', 
				'" . $babDB->db_escape_string($babBody->currentAdmGroup) . "', 
				'0',
				" . $babDB->quote($availability_lock) . "
			)
	");

            $idowner = $babDB->db_insert_id();

            $babDB->db_query("insert into " . BAB_CALENDAR_TBL . " (owner, type) values ($idowner, '" . BAB_CAL_RES_TYPE . "')");
            $idCal = $babDB->db_insert_id();
            //echo "insert into ".UNSCALENDAR_CLASSROOMS_TABLE_NAME." values(null, '$name', ".$idowner." , $buildingID, $videoprojector, $capacity, now(), ". $GLOBALS['BAB_SESS_USERID'] . " );";
            $babDB->db_query("insert into " . UNSCALENDAR_CLASSROOMS_TABLE_NAME . " values (null, '$name', " . $idCal . " , $buildingID, $videoprojector, $capacity, now(), " . $GLOBALS['BAB_SESS_USERID'] . " );");
        } else {
            $babBody->addError(bab_translate("A classroom with this name allready exist !", $GLOBALS['babAddonFolder']));
        }
    } else {
        $babBody->addError(bab_translate("All fields are required!", $GLOBALS['babAddonFolder']));
    }
}

function unsCalendar_deleteClassroom($id) {

    global $babDB;

    $classroom = unsCalendar_getClassroom($id);
    
    // delete all event for this calendar
    $res = $babDB->db_query("select id_event from " . BAB_CAL_EVENTS_OWNERS_TBL . " where id_cal='" . $classroom['calendarID'] . "'");
    while ($arr = $babDB->db_fetch_array($res)) {
        $babDB->db_query("delete from " . BAB_CAL_EVENTS_TBL . " where id='" . $babDB->db_escape_string($arr['id_event']) . "'");
        $babDB->db_query("delete from " . BAB_CAL_EVENTS_NOTES_TBL . " where id_event='" . $babDB->db_escape_string($arr['id_event']) . "'");
        $babDB->db_query("delete from " . BAB_CAL_EVENTS_REMINDERS_TBL . " where id_event='" . $babDB->db_escape_string($arr['id_event']) . "'");
    }


    $babDB->db_query("delete from " . BAB_CALENDAR_TBL . " where id =" . $classroom['calendarID']);
    $babDB->db_query("delete from " . BAB_CALENDAR_TBL . " where owner =" . $classroom['calendarID']);

    $babDB->db_query("delete from " . UNSCALENDAR_CLASSROOMS_TABLE_NAME . " where id=" . $id);
}

function unsCalendar_getClassroom($id) {
    global $babDB;

    $res = $babDB->db_query("select id, classroom_name as name, classroom_calendarID as calendarID, classroom_buildingID as buildingID, classroom_video as video, classroom_capacity as capacity from " . UNSCALENDAR_CLASSROOMS_TABLE_NAME . " where id = " . $id);

    $classroom = $babDB->db_fetch_array($res);

    return $classroom;
}

function unsCalendar_getClassroomForBuilding($id) {

    global $babDB;


    $res = $babDB->db_query("select id, classroom_name as name, classroom_capacity as capacity, classroom_video as videoprojector from " . UNSCALENDAR_CLASSROOMS_TABLE_NAME . " where classroom_buildingID = " . $id);

    $classrooms = array();
    $i = 0;
    while ($arr = $babDB->db_fetch_array($res)) {
        $classrooms[$i] = $arr;
        $i++;
    }
    return $classrooms;
}

?>
